﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using CRM.DbInterface;


namespace DBSynchronize
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             
insert into dbo.Customers
(SequentialNumber, DateStartProfineRUS, [Name], IsActive, IdCustomerClassificationsByBusiness, IdCustomerClassificationsProfine, IdCustomerClassificationsTurnover)
select SequentialNumber, DateStartProfineRUS, WellKnownName, 'true', 1,2,1 from cdb.dbo.Clients
             
             */

            //for (int index = 0; index < DataLayer.Clients.Count; index++)
            //{
            //    var item = DataLayer.Clients[index];
            //    try
            //    {
            //        Customers cust = DB.GetCustomerBySequentalNumber(item.SequentialNumber);

            //        if (cust != null)
            //        {
            //            cust.Comments = item.ClientPropertyActual.Comment;
            //            // cust.SequentialNumber = item.SequentialNumber;
            //            cust.DateStartProfineRUS = item.DateStartProfineRus;
            //            //cust.Name = item.WellKnownName;
            //            cust.IsActive = true;
            //            //cust.profineRegions =
            //            //    DB.GetprofineRegionbyName(item.ClientPropertyActual.ProfineRegion.Name);
            //            cust.IdprofineRegion =
            //                DB.GetprofineRegionbyName(item.ClientPropertyActual.ProfineRegion.Name).IdProfineRegion;
            //            cust.IdCustomerClassificationsByBusiness = 1;
            //            cust.IdCustomerClassificationsProfine = 2;
            //            cust.IdCustomerClassificationsTurnover = 1;
            //            cust.IdUser = item.ClientPropertyActual.User;
            //            cust.IdManager = item.Manager.Login;

            //            // DB.Entities.SaveChanges();
            //        }

            //        //  DB.Customers.AddObject(cust);
            //    }
            //    catch
            //    {
            //    }
            //    finally
            //    {
            //        Console.Clear();
            //        Console.WriteLine("Customers   Index: " + index + " of " + DataLayer.Clients.Count);
            //    }
            //}


            //DB.Entities.SaveChanges();

            //for (int i = 0; i < DataLayer.ClientProperties.Count; i++)
            //{
            //    var item = DataLayer.ClientProperties[i];
            //    try
            //    {
            //        LegalEntities le = new LegalEntities();
            //        le.IsActive = true;
            //        le.IdUser = @"ru\28prygun01";
            //        le.Name = item.Name;

            //        Customers customers = DB.GetCustomerBySequentalNumber(item.Client.SequentialNumber);

            //        le.IdCustomer = customers.IdCustomer;

            //        DB.Entities.LegalEntities.AddObject(le);




            //    }
            //    catch
            //    {
            //    }

            //    finally
            //    {
            //        Console.Clear();
            //        Console.WriteLine("LegalEntities   Index: " + i + " of " + DataLayer.ClientProperties.Count);
            //    }


            //}


            //DB.Entities.SaveChanges();

            //for (int index = 0; index < DataLayer.Contactors.Count; index++)
            //{
            //    var cont = DataLayer.Contactors[index];
            //    try
            //    {
            //        Contacts c = new Contacts();
            //        c.Name = cont.Name;
            //        if (c.Name.Length > 100)
            //            c.Name = c.Name.Remove(0, c.Name.Length - 99);
            //        c.IdLegalEntity =
            //            DB.GetCustomerBySequentalNumber(cont.Client.SequentialNumber).LegalEntities.FirstOrDefault().
            //                IdLegalEntity;
            //        c.Position = cont.Post;
            //        c.Phone = cont.PhoneNumber;
            //        c.Email = cont.Email;
            //        c.Remind = cont.ShouldBeCongratulatedWithBD.ToString();
            //        c.Comments = cont.Comment;
            //        c.IsActive = true;
            //        c.Birthday = cont.DateOfBirth;


            //        var con = DB.Contacts.FirstOrDefault(cc => cc.Name == c.Name);

            //        if (con == null)
            //        {
            //            DB.Entities.Contacts.AddObject(c);
            //            DB.Entities.SaveChanges();
            //        }

            //        //  


            //    }
            //    catch
            //    {
            //    }
            //    finally
            //    {
            //        Console.Clear();
            //        Console.WriteLine(" Contacts    Index: " + index + " of " + DataLayer.Contactors.Count);
            //    }
            //}


            //DB.Entities.SaveChanges();

            //////for (int index = 0; index < DataLayer.Clients.ClientBySequentialNumberGet(1210).Contactors.Count; index++)
            //////{
            //////    var cont = DataLayer.Clients.ClientBySequentialNumberGet(1210).Contactors[index];
            //////    try
            //////    {
            //////        Contacts c = new Contacts();
            //////        c.Name = cont.Name;
            //////        c.IdLegalEntity =
            //////            DB.GetCustomerBySequentalNumber(cont.Client.SequentialNumber).LegalEntities.FirstOrDefault().
            //////                IdLegalEntity;
            //////        c.Position = cont.Post;
            //////        c.Phone = cont.PhoneNumber;
            //////        c.Email = cont.Email;
            //////        c.Remind = cont.ShouldBeCongratulatedWithBD.ToString();
            //////        c.Comments = cont.Comment;
            //////        c.IsActive = true;
            //////        c.Birthday = cont.DateOfBirth;


            //////        var con = DB.Contacts.FirstOrDefault(cc => cc.Name == c.Name);

            //////        if (con == null)
            //////        {
            //////            DB.Entities.Contacts.AddObject(c);
            //////            DB.Entities.SaveChanges();
            //////        }

            //////        //  


            //////    }
            //////    catch
            //////    {
            //////    }
            //////    finally
            //////    {
            //////        Console.Clear();
            //////        Console.WriteLine(" Contacts    Index: " + index + " of " + DataLayer.Contactors.Count);
            //////    }
            //////}


            //////DB.Entities.SaveChanges();

            //for (int index = 0; index < DataLayer.ClientProperties.Count; index++)
            //{
            //    var item = DataLayer.ClientProperties[index];


            //    try
            //    {
            //        Addresses ad = new Addresses();
            //        ad.IdLegalEntity =
            //            DB.GetCustomerBySequentalNumber(item.Client.SequentialNumber).GetLegalEntityByName(item.Name).
            //                IdLegalEntity;
            //        ad.ZipCode = item.PostIndex;
            //        ad.IdAddressesItem = item.IdReferenceSettlement;
            //        var firstOrDefault = item.Client.Phones.FirstOrDefault();
            //        if (firstOrDefault != null)
            //            ad.Phone = firstOrDefault.Fax;
            //        else ad.Phone = "";
            //        var orDefault = item.Client.Faxes.FirstOrDefault();
            //        if (orDefault != null)
            //            ad.Fax = orDefault.Fax;
            //        else ad.Fax = "";
            //        ad.IsActive = true;
            //        ad.IdAddressesType = 1;
            //        DB.Entities.Addresses.AddObject(ad);
            //        DB.Entities.SaveChanges();

            //    }
            //    catch
            //    {

            //    }
            //    finally
            //    {
            //        Console.Clear();
            //        Console.WriteLine("Address  Index: " + index + " of " + DataLayer.ClientProperties.Count);
            //    }
            //}


       //     DB.Entities.SaveChanges();

            //for (int index = 0; index < DataLayer.Rates.Count; index++)
            //{
            //    var rate = DataLayer.Rates[index];
            //    try
            //    {
            //        var turn = new Turnovers();

            //        turn.Amount = (float)rate.Amount;
            //        turn.Period = rate.Period;
            //        turn.IdLegalEntity =
            //            DB.GetCustomerBySequentalNumber(rate.Client.SequentialNumber).LegalEntities.FirstOrDefault().
            //                IdLegalEntity;

            //        switch (rate.IdRateCathegory)
            //        {
            //            case 1:
            //                //Дилерский оборот

            //                turn.IdTurnoverClassification = 1;

            //                turn.IdDealerLegalEntity =
            //                    DB.GetCustomerBySequentalNumber(rate.Dialer.SequentialNumber).LegalEntities.
            //                        FirstOrDefault().
            //                        IdLegalEntity;
            //                break;
            //            case 2:
            //                //Оборот из Германии
            //                turn.IdTurnoverClassification = 3;
            //                turn.IdDealerLegalEntity = 52;
            //                break;
            //            case 3:
            //                //Оборот из Воскресенска
            //                turn.IdTurnoverClassification = 3;
            //                turn.IdDealerLegalEntity = 51;
            //                break;
            //            case 4:
            //                //Балтика
            //                turn.IdDealerLegalEntity =
            //                    DB.GetCustomerBySequentalNumber(rate.Dialer.SequentialNumber).LegalEntities.
            //                        FirstOrDefault().
            //                        IdLegalEntity;
            //                break;
            //            case 5:
            //                //Производство
            //                turn.IdTurnoverClassification = 2;
            //                break;
            //        }

            //        DB.Entities.Turnovers.AddObject(turn);


            //    }
            //    catch
            //    {
            //    }
            //    finally
            //    {
            //        Console.Clear();
            //        Console.WriteLine("Turnovers  Index: " + index + " of " + DataLayer.Rates.Count);
            //    }
            ////}
            //DB.Entities.SaveChanges();


            /*Перенос клиентов из crm v cdb*/
            foreach (var customer in DB.Customers)
            {
                var client = DataLayer.Clients.ClientBySequentialNumberGet(customer.SequentialNumber);

                if (client == null)
                {
                    if (customer.IdprofineRegion != null)
                        client = new Client(customer.SequentialNumber, DateTime.Now, customer.DateStartProfineRUS, false, DateTime.Now, DateTime.Now, "", "", "", "", customer.Name, DataLayer.OwnershipPatterns[0], DataLayer.Users[0], DataLayer.Regions[0], "", "", "", "", "", DataLayer.ClientQualifications[0], DataLayer.ClientClassifications[0], DataLayer.RelationTypes[0], DataLayer.ProfineRegions.profineRegionByNameGet(DB.GetprofineRegionbyId((int)customer.IdprofineRegion).NameRu), customer.Name);
                    DataLayer.Clients.Add(client);


                    //if (client.ClientPropertyActual != null)
                    //    client.ClientPropertyActual.Name = customer.LegalEntities.FirstOrDefault().Name;

                    DataLayer.Save();
                }


            }

            ///*Синхронизация городов и улиц клиента*/
            //var i = 0;

            //foreach(var customer in DB.Customers)
            //{
            //    i++;
            //    Console.Clear();
            //    Console.WriteLine("Customers  Index: " + i + " of " + DB.Customers.Count());

            //    var client = DataLayer.Clients.ClientBySequentialNumberGet(customer.SequentialNumber);

            //    if(client!=null)
            //    {
            //        foreach(var clientProperty in client.ClientProperties)
            //        {
            //            var legalentity = customer.LegalEntities.FirstOrDefault(p => p.Name == clientProperty.Name);
            //            if(legalentity==null)
            //            {
            //                legalentity = customer.LegalEntities.FirstOrDefault();
            //            }
            //            if(legalentity!=null)
            //            {
            //                var newAddress = new Addresses();
            //                newAddress.IdLegalEntity = legalentity.IdLegalEntity;
            //                newAddress.IdAddressesItem = clientProperty.IdReferenceSettlement;
            //                newAddress.Comments = clientProperty.Address;
            //                newAddress.IdAddressesType = 1;
            //                newAddress.IdUser = "max";

            //                DB.Entities.Addresses.AddObject(newAddress);

            //                DB.Save();
            //            }
            //        }
            //    }
            //}

            /**********************/

            //for (int index = 0; index < DataLayer.Rates.Count; index++)
            //{
            //    var rate = DataLayer.Rates[index];

            //    var turn = new Turnovers();

            //    turn.Amount = (float)rate.Amount;
            //    turn.Period = rate.Period;
                
            //    if((turn.Period.Year==2012 & turn.Period.Month<9) | (turn.Period.Year<2012))
            //    {
            //        turn.IdTurnoverClassification = 1;
            //        turn.IdLegalEntity = DB.GetCustomerBySequentalNumber(rate.Client.SequentialNumber).LegalEntities.FirstOrDefault().IdLegalEntity;

            //        if (rate.Dialer.SequentialNumber == 7441)//Волгоград
            //        {
            //            turn.IdDealerLegalEntity = 9902;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7438)//Челябинск
            //        {
            //            turn.IdDealerLegalEntity = 9917;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7436)//Саратов
            //        {
            //            turn.IdDealerLegalEntity = 9916;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7433)//Самара
            //        {
            //            turn.IdDealerLegalEntity = 9915;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7434)//Ростов-на-Доеу
            //        {
            //            turn.IdDealerLegalEntity = 9914;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7432)//Пермь
            //        {
            //            turn.IdDealerLegalEntity = 9912;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7431)//Новосибирск
            //        {
            //            turn.IdDealerLegalEntity = 9911;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7430)//Нижний Новгород
            //        {
            //            turn.IdDealerLegalEntity = 9910;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7429)//Красноярск
            //        {
            //            turn.IdDealerLegalEntity = 9907;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7428)//Мин Воды
            //        {
            //            turn.IdDealerLegalEntity = 9908;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7427)//Краснодар
            //        {
            //            turn.IdDealerLegalEntity = 9906;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7114)//ЕКатеринбург
            //        {
            //            turn.IdDealerLegalEntity = 9904;
            //        }

            //        if (rate.Dialer.SequentialNumber == 6648)//Воронеж
            //        {
            //            turn.IdDealerLegalEntity = 9903;
            //        }

            //        if (rate.Dialer.SequentialNumber == 450)//Москва
            //        {
            //            turn.IdDealerLegalEntity = 7979;
            //        }

            //        if (rate.Dialer.SequentialNumber == 7426)//Казань
            //        {
            //            turn.IdDealerLegalEntity = 9905;
            //        }

            //        if (rate.Dialer.SequentialNumber == 3360)//санкт-петербург
            //        {
            //            turn.IdDealerLegalEntity = 9913;
            //        }




            //        if (turn.IdDealerLegalEntity != null)
            //        {
            //            DB.Entities.Turnovers.AddObject(turn);
            //            DB.Save();
            //        }
            //    }

            //       Console.Clear();
            //    Console.WriteLine("Turnovers  Index: " + index + " of " + DataLayer.Rates.Count);

            //}

            //Customers cust = DB.GetCustomerBySequentalNumber(3360);

            //var turn = new Turnovers();

            ////        turn.Amount = (float)rate.Amount;
            //turn.Period = rate.Period;
            //turn.IdLegalEntity =
            //    DB.GetCustomerBySequentalNumber(rate.Client.SequentialNumber).LegalEntities.FirstOrDefault().
            //        IdLegalEntity;
            //turn.IdTurnoverClassification = 1;

            //turn.IdDealerLegalEntity =
            //    DB.GetCustomerBySequentalNumber(rate.Dialer.SequentialNumber).LegalEntities.
            //        FirstOrDefault().
            //        IdLegalEntity;


//            int managerId = 362;
//            int[] customerNumber = {
//6990,
//4492,
//4445,
//8965,
//6027,
//6155,
//6156,
//6628,
//5553,
//8319,
//6002,
//6852,
//8502,
//6988,
//6992,
//7559,
//3741,
//6170,
//7785,
//5730,
//6862,
//564,
//6688,
//6590,
//6173,
//6591,
//9024,
//8907,
//6430,
//6169,
//6710,
//7290,
//6458,
//1619,
//6157,
//6022,
//1620,
//6427,
//9025,
//8175,
//8908,
//7690,
//5050,
//5731,
//6043,
//7234,
//1004,
//7689,
//4640,
//8719,
//7281,
//6174,
//6632,
//8194,
//5037,
//5997,
//1519,
//8082,
//7473,
//8779,
//5518,
//8751,
//8320,
//3268,
//8228,
//4978,
//8083,
//8411,
//8505,
//6438,
//6034,
//7472,
//7911,
//8227,
//7291,
//4194,
//6166,
//4630,
//6165,
//4245,
//1259,
//7479,
//6044,
//8840,
//6743,
//6175,
//6746,
//6024,
//5002,
//5185,
//6865,
//8720,
//1571,
//6162,
//6035,
//6629,
//8716,
//4439,
//6687,
//1604,
//6439,
//6025,
//4467,
//7471,
//7691,
//6033,
//6003,
//5999,
//1575,
//9026,
//9027,
//6991,
//5051,
//6143,
//7910,
//6176,
//7789,
//6172,
//7970,
//4665,
//3277,
//7897,
//7123,
//6171,
//8193,
//7614,
//6464,
//5621,
//8752,
//8321,
//6001,
//1258,
//6493,
//7236,
//8780,
//4957,
//465,
//6461
//};

//            foreach(var num in customerNumber)
//            {
//                var customer = DB.GetCustomerBySequentalNumber(num);
//                if (customer != null)
//                {
//                    customer.IdManager = managerId;
//                }
//            }

            

            
            DB.Save();

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
    }
}
